package com.ocr.camera;

import com.ocr.utils.StoreSetting;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class Settings extends Activity {
	private static String DEFAULT_LANGUAGE = "ENGLISH";
	Button savebutton;
	Spinner spinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		savebutton = (Button) findViewById(R.id.save);
		spinner = (Spinner) findViewById(R.id.langspinner);

		String settinglang = StoreSetting.restoreLangSetting(this);
		if (settinglang != null)
			Log.i("Lang", settinglang);
		addItemsOnSpinner(settinglang);
		initialView(settinglang);
		savebutton.setOnClickListener(saveButtonClickListener);
	}

	public void initialView(String lang) {
		TextView langlabel = (TextView) findViewById(R.id.langlabel);
		if (lang == null || lang.equals("ENGLISH")) {
			langlabel.setText("Language");
			savebutton.setText("Save");
		} else if (lang.equals("VIETNAMESE")) {
			langlabel.setText("Ngon Ngu");
			savebutton.setText("Luu");
		} else if (lang.equals("JAPANESE")) {
			langlabel.setText("言語");
			savebutton.setText("保存");
		}
	}

	public void addItemsOnSpinner(String lang) {
		String[] list = null;
		if (lang == null || lang.equals("ENGLISH")) {
			list = getResources().getStringArray(R.array.languages_array_eng);
		} else if (lang.equals("VIETNAMESE")) {
			list = getResources().getStringArray(R.array.languages_array_vie);
		} else if (lang.equals("JAPANESE")) {
			list = getResources().getStringArray(R.array.languages_array_jp);
		}
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
	}

	public void saveSetting() {

		String selang = spinner.getSelectedItem().toString();
		StoreSetting.saveLangSetting(selang, this);

	}

	OnClickListener saveButtonClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			saveSetting();
			finish();
		}
	};

}
